package com.hqq.exercise.list;

/**
 * MergeOrderedList 合并两个排序的链表
 * 题目描述:
 * 输入两个递增排序的链表，合并这两个链表并使新链表中的结点仍然是按照递增排序的
 * 思路:
 * 直接递归扫描两个链表ORZ....
 * Created by heqianqian on 2017/8/11.
 */
public class MergeOrderedList {

    public static ListNode mergeOrderedList(ListNode list1, ListNode list2) {
        if (list1 == null){
            return list2;
        }
        if (list2==null){
            return list1;
        }
        ListNode node;
        if (list1.data < list2.data) {
            node = list1;
            node.next = mergeOrderedList(list1.next, list2);
        } else {
            node = list2;
            node.next = mergeOrderedList(list1, list2.next);
        }
        return node;
    }
}
